import xlrd,xlwt
from xlutils.copy import copy
import os 
class Excel(object):
    def __init__(self, path):
        self.path = path
        self.a = []
        self.slist = []
        self.use_a = []
        self.nouse_a = []
    def delete_all_data(self,sname,lists):
        wbt = xlwt.Workbook()
        sheet = wbt.add_sheet(sname)
        i=0
        for j in lists:
            sheet.write(0,i,j)
            i+=1
        wbt.save(self.path)
    #excel的读取,传入参数:你要读取表的名字
    def get_head(self,sname):
        find_rd=xlrd.open_workbook(self.path)
        sheet=find_rd.sheet_by_name(sname)
        self.slist = sheet.row_values(0)
    # def look_data(self,sname):#查看订单
    #     find_rd=xlrd.open_workbook(self.path)
    #     sheet=find_rd.sheet_by_name(sname)
    #     for i in range(1,sheet.nrows):
    #         self.a.append(sheet.row_values(i))
    #     if self.a == []:
    #         print('当前没有订单')
    #     else:
    #         for j in self.a:
    #             print(j)
        # self.a.clear()
    def find_data(self,sname):
        find_rd=xlrd.open_workbook(self.path)
        sheet=find_rd.sheet_by_name(sname)
        for i in range(1,sheet.nrows):
            self.a.append(sheet.row_values(i))
    def correct_data(self,slist):
        add_wt=xlrd.open_workbook(self.path)
        new_add_wt=copy(add_wt)
        sheet=new_add_wt.get_sheet(0)
        for m in range(len(slist)):
            for n in range(len(slist[m])):
                sheet.write(m+1, n, slist[m][n])       
        new_add_wt.save(self.path)
    def add_date(self,slist):
        add_wt=xlrd.open_workbook(self.path)
        row=add_wt.sheets()[0].nrows
        new_add_wt=copy(add_wt)
        sheet=new_add_wt.get_sheet(0)
        i=0
        for j in slist:
            sheet.write(row,i,j)
            i+=1              
        new_add_wt.save(self.path)
    def filter_buyer(self,buyerID):
        for i in self.a:
            if i[5] == buyerID:
                self.use_a.append(i)
            else:
                self.nouse_a.append(i)
    def filter_seller(self,sellerID):
        for i in self.a:
            if i[4] == sellerID:
                self.use_a.append(i)
            else:
                self.nouse_a.append(i)           
if __name__ == "__main__":
    d = os.path.dirname(__file__)
    parent_path = os.path.dirname(d)
    file_path = parent_path + '/data/goods.xls'
    excel = Excel(file_path)
    # list_goods=['11236','小米耳环','149','60','11111']
    # excel.add_data(list_goods)
    # excel.find_data('商品信息')
